package com.ruoyi.test.serv.impl;

import com.ruoyi.test.dao.UserDao;
import com.ruoyi.test.domain.User;
import com.ruoyi.test.domain.common.Result;
import com.ruoyi.test.domain.common.ResultCodeEnum;
import com.ruoyi.test.exception.MyGuiguException;
import com.ruoyi.test.serv.IUserServ;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class IUserServImpl implements IUserServ {
    @Autowired
    UserDao dao;

    public Result<T> login(User dto) {
        String nickname = dto.getNickname();
        String password = dto.getPassword();

        if (nickname.isEmpty()) {
            throw new MyGuiguException(ResultCodeEnum.LOGIN_ERROR);
        }

        User dbUserByNickname = dao.getUserByNickname(nickname);
        if (dbUserByNickname == null) { // 如果没有查到，则为新用户
            dao.addUser(dto);
            return Result.build(null, ResultCodeEnum.SUCCESS);
        } else {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }


    }
}
